package sz.szsmk.citizencard.ui.fragment;

import android.os.Bundle;
import android.util.Log;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;


import com.google.gson.Gson;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnRefreshListener;

import org.greenrobot.eventbus.EventBus;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import sz.szsmk.citizencard.DaggerApplication;
import sz.szsmk.citizencard.R;
import sz.szsmk.citizencard.databinding.FragmentCardlistCouponBinding;
import sz.szsmk.citizencard.ioc.Compontent.DaggerCommonFragmentComponent;
import sz.szsmk.citizencard.model.network.bean.response.QueryNearStandbyRes;
import sz.szsmk.citizencard.model.network.bean.response.QueryUserCardRes;
import sz.szsmk.citizencard.model.network.dzbean.res.MessageEvent;
import sz.szsmk.citizencard.mvp.contract.IBusSiteCollectContact;
import sz.szsmk.citizencard.mvp.contract.ICardListCardsContact;
import sz.szsmk.citizencard.mvp.presenter.BusSiteCollectPresenter;
import sz.szsmk.citizencard.mvp.presenter.CardsListCardsPresenter;
import sz.szsmk.citizencard.ui.activity.CardListActivity;
import sz.szsmk.citizencard.ui.activity.IMainActivity;
import sz.szsmk.citizencard.ui.adapter.BusSiteNearbyAdapter;
import sz.szsmk.citizencard.ui.adapter.CardListAdapter;
import sz.szsmk.citizencard.ui.base.IBaseFragment;
import sz.szsmk.citizencard.util.ACache;
import sz.szsmk.citizencard.util.IoUtils;
import sz.szsmk.citizencard.util.JumpUtils;
import sz.szsmk.citizencard.widget.StickHeaderDecoration;

/**
 * Copyright (c)2018
 * author: zdh
 * created on: 2018/11/15 20:27
 * description:
 */
public class CardListCardsFragment extends IBaseFragment<CardsListCardsPresenter, FragmentCardlistCouponBinding>
        implements ICardListCardsContact.ICardListCardsView {


//    private RecyclerView collecSiteRv;
//    private BusSiteNearbyAdapter busSiteNearbyAdapter;

    private List<View> ListViewTraffic = new ArrayList<>();
    private List<View> ListViewCitizen = new ArrayList<>();
    private List<QueryUserCardRes.DataBean> listTraffic = new ArrayList<>();
    private List<QueryUserCardRes.DataBean> listCitizen = new ArrayList<>();
    private List<QueryUserCardRes.DataBean> listCardData = new ArrayList<>();

    private CardListAdapter cardListAdapter;

    public static CardListCardsFragment getInstance() {
        CardListCardsFragment sf = new CardListCardsFragment();
        return sf;
    }


    @Override
    protected int attachLayoutRes() {
        return R.layout.fragment_cardlist_coupon;
    }

    @Override
    protected void initInjector() {

        DaggerCommonFragmentComponent.builder().appCompontent(DaggerApplication.getAppComponent())
                .build().inject(this);
    }

    @Override
    protected void initViews() {

//        EventBus.getDefault().post(MessageEvent.CODE_SHOW_DIALOG);
//        mPresenter.queryUserCard("0");
        //这是增加的一条假数据，后面会清空，不然程序会报错，待优化
        QueryUserCardRes.DataBean bean = new QueryUserCardRes.DataBean();
        bean.setaCardNo("1000000");
        QueryUserCardRes.DataBean.CardDetailInfolistBean cardDetailInfolistBean = new QueryUserCardRes.DataBean.CardDetailInfolistBean();
        cardDetailInfolistBean.setCardTypeName("交通");
        List<QueryUserCardRes.DataBean.CardDetailInfolistBean> listcardIndo = new ArrayList<>();
        listcardIndo.add(cardDetailInfolistBean);
        bean.setCardDetailIdealist(listcardIndo);
        listCardData.add(bean);
        dataBinding.smartRefreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                mPresenter.queryUserCard("3");
            }
        });
        dataBinding.smartRefreshLayout.autoRefresh();
        dataBinding.addCard.setOnClickListener(v -> {
            Bundle bundle = new Bundle();
            bundle.putString("key", "home");
            JumpUtils.jumptoNatvie(getActivity(), "addnewcard", 0, bundle);
        });
    }

    @Override
    protected void updateViews() {

    }

    @Override
    public void getDataListSucc(QueryUserCardRes response){
        dataBinding.smartRefreshLayout.finishRefresh();
//        EventBus.getDefault().post(MessageEvent.CODE_DISSMISS_DIALOG);
        //从assets文件夹读取假数据
//        try {
//            String data= IoUtils.readInputStream(getActivity().getAssets().open("testData.json"));
//            response=new Gson().fromJson(data,QueryUserCardRes.class);
//        } catch (IOException e) {
//            e.printStackTrace();
//        }

//        ((CardListActivity) getActivity()).dismissLoadingDialog();
        if (response.getData().size() == 0) {
            dataBinding.textBind.setText("暂未绑卡，去绑卡");
        } else {
            dataBinding.textBind.setText("去绑卡");
        }

        listCardData.clear();
//        QueryUserCardRes.DataBean bean = new QueryUserCardRes.DataBean();
//        bean.setaCardNo("-1");
//
//        QueryUserCardRes.DataBean.CardDetailInfolistBean cardDetailInfolistBean = new QueryUserCardRes.DataBean.CardDetailInfolistBean();
//        cardDetailInfolistBean.setCardTypeName("交通");
//        List<QueryUserCardRes.DataBean.CardDetailInfolistBean> listcardIndo = new ArrayList<>();
//        listcardIndo.add(cardDetailInfolistBean);
//        bean.setCardDetailIdealist(listcardIndo);

        if (response.getData().size() > 0) {
            for (QueryUserCardRes.DataBean dataBean : response.getData()) {
                if (dataBean.getCardDetailInfolist().get(0).getCardTypeName().contains("交通"))
                    listTraffic.add(dataBean);
                if (dataBean.getCardDetailInfolist().get(0).getCardTypeName().contains("市民"))
                    listCitizen.add(dataBean);
            }

            listCardData.addAll(listTraffic);
            //增加一个尾部选项
//        listCardData.add(bean);

            listCardData.addAll(listCitizen);
            //增加一个尾部选项
//        listCardData.add(bean);
//        cardListAdapter.setViewData(listCardData);
//        cardListAdapter.notifyDataSetChanged();

            if (cardListAdapter != null) {
                cardListAdapter.notifyDataSetChanged();
            } else {
                LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
                cardListAdapter = new CardListAdapter(getActivity(), listCardData,listTraffic.size(),listCitizen.size(),true,true,0);
                dataBinding.cardRview.addItemDecoration(new StickHeaderDecoration(getActivity()));
                dataBinding.cardRview.setLayoutManager(new LinearLayoutManager(getActivity()));
                dataBinding.cardRview.setAdapter(cardListAdapter);

            }


        }
    }

    @Override
    public void getDataListFail() {
        dataBinding.smartRefreshLayout.finishRefresh();
        EventBus.getDefault().post(new MessageEvent(MessageEvent.CODE_DISSMISS_DIALOG));
//        dataBinding.collecSiteRv.onFinishRequest(false, false);
    }


    @Override
    public void onResume() {
        super.onResume();
//        mPresenter.getDataList("string","string");
//        dataBinding.collecSiteRv.startLoadFirst();
//        mPresenter.queryUserCard("3");//0 是不去重，3是查询后去重的结果
//        ((CardListActivity) getActivity()).showLoadingDialog();
    }
}
